package com.dycx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.Common;
import com.dycx.common.context.UserInfoContext;
import com.dycx.system.constants.AdminConstants;
import com.dycx.system.mapper.AdminMapper;
import com.dycx.system.dto.AdminDTO;
import com.dycx.system.dto.AdminRoleDTO;
import com.dycx.system.entity.AdminDO;
import com.dycx.system.enums.AdminTypeEnum;
import com.dycx.system.service.AdminRoleService;
import com.dycx.system.service.AdminService;
import com.dycx.system.vo.AdminVO;
import com.dycx.system.vo.RoleVO;
import lombok.AllArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class AdminImpl extends ServiceImpl<AdminMapper, AdminDO> implements AdminService {

    private AdminMapper adminMapper;
    private AdminRoleService adminRoleService;

    /**
     * 删除管理员
     *
     * @param id
     * @return
     */
    @Override
    public Boolean delete(int id) {
        isSuperAdmin(id);
        return this.removeById(id);
    }

    /**
     * 重置密码
     *
     * @param adminId
     * @return
     */
    @Override
    public String restPassword(int adminId) {
        isSuperAdmin(adminId);

        String password = Common.createRandom(8, 1);

        AdminDO adminDO = new AdminDO();
        adminDO.setId(adminId);
        adminDO.setPassword(DigestUtils.md5Hex(password + AdminConstants.PASSWORD_SALT));
        if (!this.updateById(adminDO)) {
            throw new RuntimeException("密码重置失败");
        } else {
            return password;
        }
    }

    /**
     * 管理员不可操作
     *
     * @param adminId 管理员id
     */
    public void isSuperAdmin(int adminId) {
        AdminDO adminDO = this.getById(adminId);
        if (adminDO.getUserType().equals(AdminTypeEnum.SUPER_ADMIN.getCode())) {
            throw new RuntimeException("超级管理员不可操作");
        }
    }

    /**
     * 添加
     *
     * @param adminDto
     * @return
     */
    @Override
    public String insert(AdminDTO adminDto) {
        String password = Common.createRandom(8, 1);

        AdminDO admin = BeanUtil.toBean(adminDto, AdminDO.class);
        admin.setPassword(DigestUtils.md5Hex(password + AdminConstants.PASSWORD_SALT));

        if (this.getInfoByUsername(adminDto.getUsername()) != null) {
            throw new RuntimeException("已存在相同登录用户名，请勿重复添加");
        }

        if (!this.save(admin)) {
            throw new RuntimeException("添加用户失败");
        }

        AdminRoleDTO adminRoleDto = new AdminRoleDTO();
        adminRoleDto.setAdminId(admin.getId());
        adminRoleDto.setRoleIds(adminDto.getRoleIds());
        if (!adminRoleService.insert(adminRoleDto)) {
            throw new RuntimeException("添加角色失败");
        }

        adminDto.setId(admin.getId());
        return password;
    }

    /**
     * 添加/修改
     *
     * @param adminDto
     * @return
     */
    @Override
    public Boolean update(AdminDTO adminDto) {
        isSuperAdmin(adminDto.getId());

        AdminRoleDTO adminRoleDto = new AdminRoleDTO();
        adminRoleDto.setAdminId(adminDto.getId());
        adminRoleDto.setRoleIds(adminDto.getRoleIds());
        if (!adminRoleService.insert(adminRoleDto)) return false;

        AdminDO admin = BeanUtil.toBean(adminDto, AdminDO.class);
        return this.updateById(admin);
    }

    /**
     * 更新状态
     *
     * @param adminDto
     * @return
     */
    @Override
    public Boolean updateStatus(AdminDTO adminDto) {
        isSuperAdmin(adminDto.getId());

        AdminDO admin = new AdminDO();
        admin.setId(adminDto.getId());
        admin.setStatus(adminDto.getStatus());
        return this.updateById(admin);
    }

    @Override
    public AdminVO getInfoById(Integer id) {
        return adminMapper.getInfoById(id);
    }

    @Override
    public List<AdminVO> getList(Integer status) {
        return adminMapper.getList(status);
    }

    @Override
    public AdminDO getInfoByUsername(String username) {
        return adminMapper.getInfoByUsername(username);
    }

    @Override
    public Page<AdminVO> getPage(AdminDTO adminDTO) {
        Page<AdminVO> page = new Page<>(adminDTO.getPageIndex(), adminDTO.getPageSize());  // 查询第1页，每页返回5条

        Page<AdminVO> pageData = adminMapper.getPage(page, adminDTO);

        List<AdminVO> dataList = pageData.getRecords();
        for (AdminVO adminVo : dataList) {
            adminVo.setRoleList(this.getAdminRoleList(adminVo.getId()));
            AdminTypeEnum userType = AdminTypeEnum.getCode(adminVo.getUserType());

            adminVo.setUserTypeName(userType.getName());
            adminVo.setUserTypeStyle(userType.getStyle());
        }

        pageData.setRecords(dataList);

        return pageData;
    }

    @Override
    public List<RoleVO> getAdminRoleList(Integer adminId) {
        return adminMapper.getAdminRoleList(adminId);
    }


    /**
     * 修改密码
     *
     * @param adminDto
     * @param
     * @return
     */
    @Override
    public Boolean updatePassword(AdminDTO adminDto) {
        AdminDO adminDO = new AdminDO();
        adminDO.setId(UserInfoContext.get().getUserId());
        adminDO.setPassword(DigestUtils.md5Hex(adminDto.getPassword() + AdminConstants.PASSWORD_SALT));
        return this.updateById(adminDO);
    }


}
